package design.util;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class RedisUtils {
    //定时周期执行指定的任务
    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    private AtomicInteger stock = new AtomicInteger(0);

    public RedisUtils() {
        //command：执行线程
        //initialDelay：初始化延时
        //period：两次开始执行最小间隔时间
        //unit：计时单位 MICROSECONDS:微秒 1s = 1000000微妙
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            // 模拟库存使用
            stock.addAndGet(1);
        }, 0, 100000, TimeUnit.MICROSECONDS);

    }

    public int getStockUsed() {
        return stock.get();
    }
}
